//
//  ShareGetOutView.m
//  quxiangtao
//
//  Created by HHuajiang on 2019/1/2.
//  Copyright © 2019 shanhaijin. All rights reserved.
//

#import "ShareGetOutView.h"
@interface ShareGetOutView ()

@property (nonatomic, weak) UIImageView *QRCodeImage;
@property (nonatomic, weak) UIImageView *shareShopImage;
@property (nonatomic, weak) UILabel *oldPriceLabel;
@property (nonatomic, weak) UILabel *newaPriceLabel;
@property (nonatomic, weak) UILabel *titleL;
@property (nonatomic, weak) UILabel *quanaLabel;

@end

@implementation ShareGetOutView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UIImageView *shopImage = [[UIImageView alloc] init];
        shopImage.backgroundColor = Color999999;
        [self addSubview:shopImage];
        [shopImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.mas_equalTo(0);
            make.height.mas_equalTo(jisuan(750));
        }];
        self.shareShopImage = shopImage;
        
        UIImageView *qrcodeView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"qrimagebj"]];
        [self addSubview:qrcodeView];
        [qrcodeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-jisuan(20));
            make.top.equalTo(shopImage.mas_bottom).offset(jisuan(40));
            make.size.mas_equalTo(CGSizeMake(jisuan(276), jisuan(326)));
        }];
        
        UIImageView *qrcodeImage = [[UIImageView alloc] init];
        qrcodeImage.backgroundColor = Color999999;
        [qrcodeView addSubview:qrcodeImage];
        [qrcodeImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(qrcodeView.mas_centerX);
            make.top.mas_equalTo(jisuan(20));
            make.size.mas_equalTo(CGSizeMake(jisuan(225), jisuan(225)));
        }];
        self.QRCodeImage = qrcodeImage;
        
        UILabel *recognitionLabel = [[UILabel alloc] init];
        recognitionLabel.text = @"长按识别二维码";
        recognitionLabel.font = Font(24);
        recognitionLabel.textColor = Colordddddd;
        [qrcodeView addSubview:recognitionLabel];
        [recognitionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(qrcodeView.mas_bottom).offset(-jisuan(30));
            make.centerX.equalTo(qrcodeImage.mas_centerX);
        }];
        
        UILabel *titleLabela = [[UILabel alloc] init];
        titleLabela.font = Font(28);
        titleLabela.textColor = Color333333;
        titleLabela.numberOfLines = 3;
        [self addSubview:titleLabela];
        [titleLabela mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(jisuan(38));
            make.top.equalTo(shopImage.mas_bottom).offset(jisuan(50));
            make.right.equalTo(qrcodeImage.mas_left).offset(-jisuan(40));
        }];
        
        UILabel *bottomLabel = [[UILabel alloc] init];
        bottomLabel.textColor = Colorcccccc;
        bottomLabel.textAlignment = NSTextAlignmentCenter;
        bottomLabel.font = Font(24);
        bottomLabel.text = @"爱淘帮-一款让你爱上购物的APP";
        [self addSubview:bottomLabel];
        [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(jisuan(83));
            make.bottom.mas_equalTo((IS_IPHONE_X || IS_IPHONE_Xr || IS_IPHONE_Xs || IS_IPHONE_Xs_Max) ? -20 : 0);
        }];
        
        UIView *linell = [[UIView alloc] init];
        linell.backgroundColor = Colordddddd;
        [self addSubview:linell];
        [linell mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.bottom.equalTo(bottomLabel.mas_top);
            make.height.mas_equalTo(0.5);
        }];
        
        UIImageView *quanImage = [[UIImageView alloc] init];
        quanImage.image = [UIImage imageNamed:@"vuochersBG"];
        [self addSubview:quanImage];
        [quanImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(qrcodeView.mas_left).offset(-Margin);
            make.bottom.equalTo(qrcodeView.mas_bottom).offset(-jisuan(30));
            make.size.mas_equalTo(CGSizeMake(jisuan(160), jisuan(60)));
        }];
        
        UILabel *oldLabel = [[UILabel alloc] init];
        oldLabel.textColor = Color999999;
        oldLabel.font = Font(24);
        [self addSubview:oldLabel];
        [oldLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(jisuan(38));
            make.bottom.equalTo(quanImage.mas_bottom).offset(-2);
        }];
        
        UILabel *newPriceLabel = [[UILabel alloc] init];
        newPriceLabel.textColor = Colorff0000;
        [self addSubview:newPriceLabel];
        [newPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(jisuan(38));
            make.bottom.equalTo(oldLabel.mas_top).offset(-2);
        }];
        
        UILabel *quanLabel = [[UILabel alloc] init];
        quanLabel.textColor = Colorff0000;
        quanLabel.textAlignment = NSTextAlignmentCenter;
        [quanImage addSubview:quanLabel];
        [quanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(quanImage.mas_centerY);
            make.right.mas_equalTo(-jisuan(60));
            make.width.mas_equalTo(jisuan(95));
            make.height.mas_equalTo(jisuan(60));
        }];
        
        UIImageView *iiimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"quanhoujiaImage"]];
        [self addSubview:iiimage];
        [iiimage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(jisuan(80), jisuan(30)));
            make.left.mas_equalTo(jisuan(38));
            make.bottom.equalTo(newPriceLabel.mas_top).offset(-jisuan(24));
        }];
        
        self.quanaLabel = quanLabel;
        self.oldPriceLabel = oldLabel;
        self.newaPriceLabel = newPriceLabel;
        self.titleL = titleLabela;
        
    }
    return self;
}

- (void)setShareQRImageUrl:(NSString *)shareQRImageUrl {
    _shareQRImageUrl = shareQRImageUrl;
    
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    NSString *string = shareQRImageUrl;
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    [filter setValue:data forKeyPath:@"inputMessage"];
    CIImage *image = [filter outputImage];
    UIImage *img =[self createNonInterpolatedUIImageFormCIImage:image withSize:jisuan(225)];
    
    UIGraphicsBeginImageContext(img.size);
    [img drawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];
    UIImage *centerImg = [UIImage imageNamed:@"qrcenterimage"];
    CGFloat centerW=20;
    CGFloat centerH=centerW;
    CGFloat centerX=(img.size.width-centerW)*0.5;
    CGFloat centerY=(img.size.height -centerH)*0.5;
    [centerImg drawInRect:CGRectMake(centerX, centerY, centerW, centerH)];
    UIImage *finalImg=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.QRCodeImage.image = finalImg;
}

- (void)setShareUrl:(NSString *)shareUrl {
    _shareUrl = shareUrl;
    [self.shareShopImage sd_setImageWithURL:[NSURL URLWithString:shareUrl]];
}

- (void)setListModel:(TbGoodsListModel *)listModel {
    _listModel = listModel;
    
    NSString *oldPrice = [NSString stringWithFormat:@"￥%@", listModel.goods_price];
    NSDictionary *attributeDic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *oldPirceAttributeStr = [[NSMutableAttributedString alloc] initWithString:oldPrice attributes:attributeDic];
    self.oldPriceLabel.attributedText = oldPirceAttributeStr;
    
    NSString *price = [NSString stringWithFormat:@"￥%@",listModel.zk_final_price];
    NSMutableAttributedString *priceAttributeStr = [[NSMutableAttributedString alloc] initWithString:price];
    [priceAttributeStr addAttribute:NSFontAttributeName value:Font(20) range:NSMakeRange(0, 1)];
    [priceAttributeStr addAttribute:NSFontAttributeName value:Font(30) range:NSMakeRange(1, price.length - 1)];
    self.newaPriceLabel.attributedText = priceAttributeStr;
    
    
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:listModel.title];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:3];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [listModel.title length])];
    [self.titleL setAttributedText:attributedString];
    
    NSString *quanprice = [NSString stringWithFormat:@"￥%@",listModel.coupon_amount];
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:quanprice];
    [attributeStr addAttribute:NSFontAttributeName value:Font(20) range:NSMakeRange(0, 1)];
    [attributeStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:jisuan(30)] range:NSMakeRange(1, quanprice.length - 1)];
    self.quanaLabel.attributedText = attributeStr;
    
}

- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
 {
     CGRect extent = CGRectIntegral(image.extent);
     CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
     size_t width = CGRectGetWidth(extent) * scale;
     size_t height = CGRectGetHeight(extent) * scale;
     CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
     CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
     CIContext *context = [CIContext contextWithOptions:nil];
     CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
     CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
     CGContextScaleCTM(bitmapRef, scale, scale);
     CGContextDrawImage(bitmapRef, extent, bitmapImage);
     CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
     CGContextRelease(bitmapRef);
     CGImageRelease(bitmapImage);
     return [UIImage imageWithCGImage:scaledImage];
 }

@end
